<?php

namespace app\api\service;

use think\facade\Db;

class SpikeOrderService
{
    /**
     * 获取单规格积分商品信息
     * */
    public static function findSingleSpikeGoods($goods_id)
    {
        $goods =  Db::name('goods')
            ->alias('a')
            ->field('b.goods_id,a.goods_name,a.spec_type,a.main_image,a.sell_price,b.spike_stock,b.spike_price')
            ->join('spike_goods b','b.goods_id=a.id')
            ->where(['b.id' => $goods_id,'b.status' => 1])
            ->find();

        return  $goods;
    }

    /**
     * 获取场次信息
     * */
    public static function getSpikeSessionTime($spike_session_id)
    {
        $end_time = Db::name('spike_session')->where('id', $spike_session_id)->value('end_time');
        return $end_time;
    }

    /**
     * 获取多规格商品信息
     * */
    public static function findManySpikeGoods($sku_id)
    {
        $goods = Db::name('goods_item')
            ->field('a.goods_id,a.attr,a.image,a.sell_price,b.goods_name,b.main_image,b.spec_type,c.spike_price,c.spike_stock')
            ->alias('a')
            ->join('goods b','a.goods_id=b.id')
            ->join('spike_goods_item c','a.id=c.sku_id')
            ->join('spike_goods d','d.id=c.spike_goods_id')
            ->where(['c.id' => $sku_id,'d.status' => 1])
            ->find();

        return  $goods;
    }


    /**
     * 保存订单信息
     *
     * */
    public static function saveOrder($user_id,$param,$goods,$real_amount,$order_no)
    {

        Db::startTrans();

        try {
            $order_id =  Db::name('order')->insertGetId([
                'user_id' => $user_id,
                'store_id' => $param['store_id'],
                'order_type' => 2,
                'order_no' =>  $order_no,
                'payment_type' => $param['payment_type'],
                'discount_amount' => 0,
                'real_amount' => $real_amount,
                'redeem_integral' => 0,
                'receiver_name' => $param['receiver_name'],
                'receiver_phone' => $param['receiver_phone'],
                'give_integral' => 0,
                'province' => $param['province'],
                'city' => $param['city'],
                'county' => $param['county'],
                'address' => $param['address'],
                'remark' => !empty($param['remark']) ? $param['remark'] : '',
                'status' => 1,
                'create_time' => time(),
            ]);

            Db::name('order_item')->insert([
                'order_id' => $order_id,
                'goods_id' => $goods['goods_id'],
                'goods_name' => $goods['goods_name'],
                'attr' =>  !empty($goods['attr']) ? $goods['attr'] : '',
                'image' => $goods['image'],
                'market_price' => $goods['sell_price'],
                'sell_price' => $goods['spike_price'],
                'buy_num' => $param['buy_num'],
                'total_amount' => $real_amount,
                'redeem_integral' => 0
            ]);
            // 提交事务
            Db::commit();
            return  $order_id;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return  null;
        }

    }


}